<template>
  <el-card>
    <el-form :inline="true">
      <el-form-item label="一级分类">
        <!-- change:选中值发生变化时触发 -->
        <el-select v-model="categoryStore.c1Id" @change="blur">
          <!-- label:即为展示数据 value:即为select下拉菜单收集的数据 -->
          <el-option
            v-for="(item, index) in categoryStore.c1Arr"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="二级分类">
        <el-select v-model="categoryStore.c2Id" @change="blur2">
          <el-option
            v-for="(item, index) in categoryStore.c2Arr"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="三级分类">
        <el-select v-model="categoryStore.c3Id">
          <el-option
            v-for="(item, index) in categoryStore.c3Arr"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script setup lang="ts">
//引入组件挂载完毕方法
import { onMounted } from "vue";
//引入分类相关的仓库
import useCategoryStore from "@/store/modules/category";
let categoryStore = useCategoryStore();
const getC1 = () => {
  //通知分类仓库发请求获取一级分类的数据
  categoryStore.getC1();
};
onMounted(() => {
  getC1();
});
// 获取二级分类的值 这里边是一级分类的id 已经二级分类的数据
const blur = (value: Number) => {
  categoryStore.c2Id = "";
  categoryStore.c3Id = "";
  categoryStore.c3Arr = [];
  console.log(value, "选择框的值");
  categoryStore.getC2();
};
// 获取三级分类的值 这里边是二级分类的id 已经三级分类的数据
const blur2 = (value: Number) => { 
  categoryStore.c3Id = "";
  categoryStore.getC3()
}
</script>

<style scoped>
/* 全局样式文件 */
.el-select {
  width: 200px !important;
}
</style>